//npm i cookie-parser

//导入express
const express = require('express');
//导入cookie-parser包
const cookieParser = require('cookie-parser');


//创建应用对象
const app = express();

//设置cookieParser中间件
app.use(cookieParser());

//设置cookie
app.get('/set-cookie', (req, res) => {
  //设置cookie,不带时效性，默认一次session
  res.cookie('username', 'jack');
  //带时效性，10秒
  res.cookie('pwd', '123456', { maxAge: 10 * 1000 });
  //带时效性,1小时
  res.cookie('email', '111@qq.com', { maxAge: 60 * 60 * 1000 });
  //响应
  res.send('设置cookie成功');
});

//读取cookie
app.get('/get-cookie', (req, res) => {
  //读取cookie
  console.log(req.cookies);
  let { username, pwd, email } = req.cookies;
  //响应体
  res.send(`username:${username},pwd:${pwd},email:${email}`);

});

//删除cookie
app.get('/del-cookie', (req, res) => {
  //删除cookie
  res.clearCookie('username');
  //响应
  res.send('删除cookie成功');
});

//监听端口，启动服务
app.listen(3000, () => {
  console.log('服务已经启动，3000端口监听中...');
});